﻿
#include "FileTool.h"
#include <QDir>
#include <QFileInfo>
#include <QString>
#include <QFile>
#include <QtGlobal>
#include <QDebug>

bool CopyDirRecursively(const QString &srcFilePath,  
                            const QString &tgtFilePath)  
{  
    QFileInfo srcFileInfo(srcFilePath);  
    if (srcFileInfo.isDir()) {  
        QDir targetDir(tgtFilePath);  
        targetDir.cdUp();  
        if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))  
            return false;  
        QDir sourceDir(srcFilePath);  
        QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);  
        foreach (const QString &fileName, fileNames) {  
            const QString newSrcFilePath  
                    = srcFilePath + QLatin1Char('/') + fileName;  
            const QString newTgtFilePath  
                    = tgtFilePath + QLatin1Char('/') + fileName;  
            if (!CopyDirRecursively(newSrcFilePath, newTgtFilePath))  
                return false;  
        }  
    } else {  
        if (!QFile::copy(srcFilePath, tgtFilePath))  
            return false;  
    }  
    return true;  
}

bool DeleteDir(const QString &dirName)  
{  
    QDir directory(dirName);  
    if (!directory.exists())  
    {  
        return true;  
    }  
  
    QString srcPath = QDir::toNativeSeparators(dirName);  
    if (!srcPath.endsWith(QDir::separator()))  
        srcPath += QDir::separator();  
  
    QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);  
    bool error = false;  
    for (QStringList::size_type i=0; i != fileNames.size(); ++i)  
    {  
        QString filePath = srcPath + fileNames.at(i);  
        QFileInfo fileInfo(filePath);  
        if (fileInfo.isFile() || fileInfo.isSymLink())  
        {  
            QFile::setPermissions(filePath, QFile::WriteOwner);  
            if (!QFile::remove(filePath))  
            {  
                qDebug() << "remove file" << filePath << " faild!";  
                error = true;  
            }  
        }  
        else if (fileInfo.isDir())  
        {  
            if (!DeleteDir(filePath))  
            {  
                error = true;  
            }  
        }  
    }  
  
    if (!directory.rmdir(QDir::toNativeSeparators(directory.path())))  
    {  
        qDebug() << "remove dir" << directory.path() << " faild!";  
        error = true;  
    }  
  
    return !error;  
}  

bool DeleteDirContent(const QString& dirName){
    QDir directory(dirName);  
    if (!directory.exists())  
    {  
        return true;  
    }  
  
    QString srcPath = QDir::toNativeSeparators(dirName);  
    if (!srcPath.endsWith(QDir::separator()))  
        srcPath += QDir::separator();  
  
    QStringList fileNames = directory.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);  
    bool error = false;  
    for (QStringList::size_type i=0; i != fileNames.size(); ++i)  
    {  
        QString filePath = srcPath + fileNames.at(i);  
        QFileInfo fileInfo(filePath);  
        if (fileInfo.isFile() || fileInfo.isSymLink())  
        {  
            QFile::setPermissions(filePath, QFile::WriteOwner);  
            if (!QFile::remove(filePath))  
            {  
                qDebug() << "remove file" << filePath << " faild!";  
                error = true;  
            }  
        }  
        else if (fileInfo.isDir())  
        {  
            if (!DeleteDir(filePath))  
            {  
                error = true;  
            }  
        }  
    }  
  
    return !error;  
}
